<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


/**
 * @package		Joomla
 * @subpackage	JobBoard
 */
class JobBoardTableJA_Applications extends JTable
{
    
    /** @var int Primary key */
    var $id             = null;
    var $attachment     = null;
    var $apply_type 	= null;
    var $inlinked		= null;
    var $params  		= null;
    var $confirm_email_when_read = 0;
//    var $posted         = null;
	/**
	* @param database A database connector object
	*/
	function __construct(&$db)
	{
		global $mainframe;
        
        $table_name = 'ja_applications';
        parent::__construct( "#__$table_name", 'id', $db );
        
        // Get other field
        $query = 
            "SELECT id, field_name, field_type, field_width, field_height "
            ." FROM  #__ja_form_fields "
            ." WHERE field_name!='' AND table_name = '". $table_name ."' AND (field_state = 'chg' OR field_state = 'add') AND published=1"
            ;
            
       $this->_db->setQuery($query);
       $fields = $this->_db->loadObjectList();

       $count = 0;
       $count_editor = 0;
       $arr_temp_editor = array();
       $arr_temp = array();
       for($i = 0, $n = count($fields); $i < $n; $i++){
           $field_name = $fields[$i]->field_name; 
           $this->$field_name = null;
           
           
           if (($fields[$i]->field_type=='IMAGE')||($fields[$i]->field_type=='FILE')||($fields[$i]->field_type=='DATE_CAL'))
           {
           	$arr_temp[$count]['name'] = $fields[$i]->field_name;
           	$arr_temp[$count]['type'] = $fields[$i]->field_type;
           	$arr_temp[$count]['field_width'] = $fields[$i]->field_width;
           	$arr_temp[$count]['field_height'] = $fields[$i]->field_height;
           	$count++;
           }           
	       if (($fields[$i]->field_type=='EDITOR'))
           {
	           	$arr_temp_editor[$count_editor] = $fields[$i]->field_name;
	           	$count_editor += 1;
           }
       }
       $this->editor_field = $arr_temp_editor;
       $this->spec_field = $arr_temp;
    }

	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		//print_r($this);exit;
//		JFilterOutput::objectHTMLSafe($this, ENT_QUOTES,'');
		$filter	= new JFilterInput( '', '', 1, 1);
		$cid = $this->id;
		$where_more = '';
		$user = JFactory::getUser();
		if($cid){			
			if(in_array($user->usertype,array('Administrator','Super Administrator'))){
				$where_more = " and (display in ('show_all', 'hide_on_new', 'admin_only', 'hide_on_edit'))";
			}
			else $where_more = " and display='show_all'";
		}
		else{
			if(in_array($user->usertype,array('Administrator','Super Administrator'))){
				$where_more = " and (display in ('show_all', 'hide_on_new', 'admin_only', 'hide_on_edit'))";
			}
			else $where_more = " and display in ('show_all', 'hide_on_edit')";
		}
		$warning_msg =JText::_("There are some invalid data. Please check again.");
        $fieldList = getEditFieldList('ja_applications', $where_more);
        for($i = 0, $n = count($fieldList); $i < $n; $i++){
            $field = $fieldList[$i];
            if ($field->field_type=='FILE'&&JRequest::getVar($field->field_name.'_'.$this->job_id))
            	$this->{$field->field_name} = JRequest::getVar($field->field_name.'_'.$this->job_id);
            	
            
            $this->{$field->field_name} = preg_replace('@<script[^>]*?.*?</script>@siu','',$this->{$field->field_name});
            $this->{$field->field_name} = $filter->clean($this->{$field->field_name});
            $this->{$field->field_name} = ltrim($this->{$field->field_name});
			$this->{$field->field_name} = rtrim($this->{$field->field_name});
            if($field->is_required){
				if(is_null($this->{$field->field_name}) || $this->{$field->field_name} == "")
                {
                	if (!$field->error_message)
                		JError::raiseWarning(0, $warning_msg);
                	else
						JError::raiseWarning(0,  JText::_($field->error_message) );
					echo $warning_msg, '<br/>'. JText::_($field->error_message).'<br/>';
                    return false;
                }
            } 
			
			
            switch($field->field_validate_type){
                case "number":
                	if($this->{$field->field_name}!='')
                    if(!is_numeric($this->{$field->field_name})){
                    	echo $warning_msg, '<br/>'. JText::_($field->error_message).'<br/>';
                    	if (!$field->error_message)
                    		JError::raiseWarning(0,$warning_msg);
                    	else
                        	JError::raiseWarning(0, JText::_($field->error_message) );
                        return false;
                    }
					break;
                case "email":
                	if($this->{$field->field_name}!='')
					if(!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $this->{$field->field_name})&&!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{3,4})$/i', $this->{$field->field_name})&&!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{4,5})$/i', $this->{$field->field_name})){
						echo $warning_msg, '<br/>'. JText::_($field->error_message) .'<br/>';
						if (!$field->error_message)
							JError::raiseWarning(0,$warning_msg);
						else
                        	JError::raiseWarning(0, JText::_($field->error_message) );
                        return false;
                    }
                    break;
                case "date":
                	if($this->{$field->field_name}!='')
                    if(!preg_match('/^[0-9]{4}([-\/][0-9]{2}){2}$/i', $this->{$field->field_name})){
                    	echo $warning_msg, '<br/>'. JText::_($field->error_message).'<br/>';
                    	if (!$field->error_message)
                    		JError::raiseWarning(0,$warning_msg);
                    	else
                        	JError::raiseWarning(0, JText::_($field->error_message) );
                        return false;
                    }
                    break;
                case "time":
                	if($this->{$field->field_name}!='')
                    if(!preg_match('/^[0-9]{2}(:[0-9]{2}){2}$/i', $this->{$field->field_name})){
                    	echo $warning_msg, '<br/>'. JText::_($field->error_message).'<br/>';
                    	if (!$field->error_message)
                    		JError::raiseWarning(0,$warning_msg);
                    	else
                        	JError::raiseWarning(0, JText::_($field->error_message));
                        return false;
                    }
                    break;
				case "datetime":
					if($this->{$field->field_name}!=''){
						$this->{$field->field_name} = date( "Y-m-d H:i:s", strtotime($this->{$field->field_name}));
	                    if(!preg_match('/^[0-9]{4}([-\/][0-9]{2}){2}[ ]+[0-9]{2}(:[0-9]{2}){2}$/i', $this->{$field->field_name})){
	                    	echo $warning_msg, '<br/>'. JText::_($field->error_message).'<br/>';
	                    	if (!$field->error_message)
	                    		JError::raiseWarning(0,$warning_msg);
	                    	else
	                        	JError::raiseWarning(0, JText::_($field->error_message));
	                        return false;
	                    }
					}
					break;
            }
            
        }
        
        
        
        return true;
	}    
	
	
}